package visual;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;

/**
 * A demonstration application showing a time series chart where you can dynamically add
 * (random) data by clicking on a button.
 *
 */
@SuppressWarnings("serial")
public class DynamicGraph extends ApplicationFrame implements ActionListener {
    
    ArrayList<TimeSeries> seriesList  = new ArrayList<TimeSeries>();

    /** The most recent value added. */
    private double lastValue = 100.0;
    
    private final TimeSeriesCollection dataset;
    
    private int index = 0;

    /**
     * Constructs a new demonstration application.
     *
     * @param title  the frame title.
     */
    @SuppressWarnings("deprecation")
	public DynamicGraph(String title, ArrayList<String> pluginNames) {

        super(title);
        dataset = new TimeSeriesCollection();
        for (String plugin : pluginNames) {
        	TimeSeries series = new TimeSeries(plugin, Millisecond.class);
        	dataset.addSeries(series);
            this.seriesList.add(series);
        }
        final JFreeChart chart = createChart(dataset);

        final ChartPanel chartPanel = new ChartPanel(chart);

        final JPanel content = new JPanel(new BorderLayout());
        content.add(chartPanel);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(content);

    }


    /**
     * Creates a sample chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return A sample chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
            "Dynamic Data Demo", 
            "Time", 
            "Value",
            dataset, 
            true, 
            true, 
            false
        );
        final XYPlot plot = result.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(0.0);
        ValueAxis range = plot.getRangeAxis();
        range.setAutoRange(true);
        range.setFixedAutoRange(0.0); 
        return result;
    }
    
    /**
     * Handles a click on the button by adding new (random) data.
     *
     * @param e  the action event.
     */
    public void refreshSeries(ArrayList<ArrayList<Integer>> datas) {
    	for(int i = 0; i < this.seriesList.size(); i++) {
	           lastValue = datas.get(i).get(this.index);
	           seriesList.get(i).addOrUpdate(new Millisecond(), lastValue);
    	}
    	this.index++;
    }
    /**
     * restarts the index
     */
    public void restart() {

        this.index = 0;
    }
    
	@Override
	public void actionPerformed(ActionEvent arg0) {
	}
	
	@Override
	public void windowClosing(final WindowEvent event) { 
        if (event.getWindow() == this) { 
            dispose(); 
        } 
    } 

}